package com.atguigu.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @author sx
 * @date 2022-02-24 11:05
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    //作者信息
    public static final Contact DEFAULT_CONTACT = new Contact("six", "https://gitee.com/hilton0224/projects", "2533564649@qq.com");

    //定制
    @Bean
    public Docket docket(Environment environment){
        /**
         * 功能点：实现测试环境启动Swagger，生产环境关闭Swagger
         * 获取项目的环境
         */
        //设置要显示swagger的环境
        Profiles profiles = Profiles.of("dev", "test");
        boolean flag = environment.acceptsProfiles(profiles);
        System.out.println("flag====>"+flag);

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("six")
                .apiInfo(apiInfo())
                //是否启用swagger，如果为false则无法浏览
                .enable(flag)
                .select()
                //配置要扫描接口的方式
                /**
                 * basePackage:扫描指定的包
                 * any:扫描全部
                 * none:不扫描
                 * withClassAnnotation(GetMapping.class):扫描类上的注解
                 * withMethodAnnotation(RestController.class):扫描方法上的注解
                 */
                .apis(RequestHandlerSelectors.basePackage("com.atguigu.controller"))
                //只扫描某个路径下
//                .paths(PathSelectors.ant("/atguigu/**"))
                .build();
    }

    //配置swagger信息
    private ApiInfo apiInfo(){
        return new ApiInfo(
                "six的Swagger日记",
                "还在路上",
                "v1.0",
                "https://gitee.com/hilton0224/projects",
                DEFAULT_CONTACT,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }


    //配置多个分组，用于协同开发
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }

}
